package ferri.lorenzo;

import java.util.LinkedList;

import android.app.ListActivity;

import android.content.Intent;
import android.database.Cursor;

import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class SongListActivity extends ListActivity {
	static final String TAG = "[SongListActivity]";
	private Cursor managedCursor;
	OptionsMenu optionsMenu = null;
	int x=0;
	LinkedList<String> file= new LinkedList<String>();
	LinkedList<String> durata = new LinkedList<String>(); 
	LinkedList<String> title = new LinkedList<String>(); 
	LinkedList<String> album = new LinkedList<String>();
	LinkedList<String> artist = new LinkedList<String>();
	LinkedList<Long> artal = new LinkedList<Long>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        String query = "%mp3";
        
        // Estraggo i dati passati all' intent
        Bundle extras = getIntent().getExtras();
        String where = new String();
        if (extras == null) {
        	// Clausola where
        	Log.v(TAG, "all song list");
        	where = MediaStore.Audio.Media.IS_MUSIC + "=1"+ " AND " + MediaStore.Audio.Media.DISPLAY_NAME
        			+ " LIKE \"" + query + "\"";;
        }
        else {
        	if (extras.containsKey(MediaStore.Audio.Media.ALBUM_ID)) {
            	// Clausola Where
            	Log.v(TAG, "song list of specific album");
            	long albumId = extras.getLong(MediaStore.Audio.Media.ALBUM_ID);
            	where = MediaStore.Audio.Media.IS_MUSIC + "=1" + 
            		" AND " + 
            		MediaStore.Audio.Media.ALBUM_ID + "=" + albumId + " AND " + MediaStore.Audio.Media.DISPLAY_NAME
        			+ " LIKE \"" + "%mp3" + "\"";
        	}
        	else if (extras.containsKey(MediaStore.Audio.Media.ARTIST_ID)) {
            	// Clausola Where
            	Log.v(TAG, "song list of specific artist");
            	long artistId = extras.getLong(MediaStore.Audio.Media.ARTIST_ID);
            	where = MediaStore.Audio.Media.IS_MUSIC + "=1" + 
            		" AND " + 
            		MediaStore.Audio.Media.ARTIST_ID + "=" + artistId + " AND " + MediaStore.Audio.Media.DISPLAY_NAME
        			+ " LIKE \"" + "%mp3" + "\"";
        	}
        	else {
        		Log.e(TAG, "unexpected intent");
        	}
        }

        // Eseguo la query 
    	managedCursor = managedQuery(
    		MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,  //Content Provider
        	new String[] {						
    			MediaStore.Audio.Media.DATA,
	       		MediaStore.Audio.Media._ID,
	    		MediaStore.Audio.Media.ARTIST,			// Artista
	    		MediaStore.Audio.Media.ARTIST_ID,		// ID_Artista
	    		MediaStore.Audio.Media.ALBUM,			// Titolo Album
	    		MediaStore.Audio.Media.ALBUM_ID,		// ID Album
	       		MediaStore.Audio.Media.TITLE,			// Titolo Canzone
	       		MediaStore.Audio.Media.DISPLAY_NAME,
	       		MediaStore.Audio.Media.DURATION
    	    },
    		where,										// WHERE
    		null,										// WHERE param
    		MediaStore.Audio.Media.DEFAULT_SORT_ORDER	// ORDER BY
    	);
    	

        ListAdapter adapter = new SimpleCursorAdapter(
        	this,									// context
        	android.R.layout.simple_list_item_multiple_choice,	// layout
        	managedCursor,							// cursor
        	new String[] {							//Colonne da visualizzare
   	       		MediaStore.Audio.Media.TITLE		//
       	    },
        	new int[] {								// to(textids)
        		android.R.id.text1, 
        	}
        );
        
        setListAdapter(adapter);
    }
    
    //Evento click di un item della lista
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
    	
    
    	managedCursor.moveToPosition(position);
      
    	file.addLast(managedCursor.getString(managedCursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
    	durata.addLast(managedCursor.getString(managedCursor.getColumnIndex(MediaStore.Audio.Media.DURATION)));
		title.addLast(managedCursor.getString(managedCursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
    	album.addLast(managedCursor.getString(managedCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
    	artist.addLast(managedCursor.getString(managedCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));
    	artal.addLast(managedCursor.getLong(managedCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)));
       	x++;
    	
     
    	
    }

    //Cattura l'evento OK ritornando le liste relative alle canzoni
    public void OK(View view) {

		
		switch (view.getId()) {
		case R.id.button_ok:
			
			
	    	
	    	Intent intent = new Intent(Intent.ACTION_VIEW);
			
			String[] path = new String[file.size()];
			String[] tit = new String[file.size()];
			String[] alb = new String[album.size()];
			String[] art = new String[artist.size()];
			String[] dur = new String[durata.size()];
			long[] albart = new long[artal.size()];
			
			for(int i=0;i<file.size();i++)
			{
				path[i]=file.get(i);
				tit[i]=title.get(i);
				alb[i]=album.get(i);
				art[i]=artist.get(i);
				dur[i]=durata.get(i);
				albart[i]=artal.get(i);
			}
			
			intent.putExtra("titolo", tit);
			intent.putExtra("album", alb);
			intent.putExtra("artist", art);
			intent.putExtra("durata", dur);
			intent.putExtra("path", path);
			intent.putExtra("art", albart);
	  
	    	setResult(RESULT_OK,intent);
	    	finish();
		}
    }
    
   

}